package com.qczl.pme.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qczl.pme.dao.AreaCmsDao;
import com.qczl.pme.dao.AreaDao;
import com.qczl.pme.dao.StbDao;
import com.qczl.pme.entity.PMEArea;
import com.qczl.pme.entity.PMEAreaCms;
import com.qczl.pme.entity.PMESTB;
import com.qczl.pme.service.AreaService;
import com.qczl.pme.vo.AreaVo;

/**
 * 区域业务逻辑层实现类
 * 
 * @author SongR
 *
 */
@Service
public class AreaServiceImpl implements AreaService {

	@Autowired
	AreaDao areadao;

	@Autowired
	StbDao stbdao;
	@Autowired
	AreaCmsDao acdao;

	/**
	 * 实现根据机顶盒获取拥有的镇级权限
	 */
	@Override
	public List<AreaVo> getTownByStbV1(String stbno, int start, int size) throws Exception {
		String sql = "select * from pme_basis_area area ,(select c.area_id from pme_basis_stb a,pme_basis_camera b,pme_basis_area c,pme_basis_stb_camera d where a.stb_id = d.stb_id and b.camera_id=d.camera_id and b.area_id=c.area_id and a.stb_no='"
				+ stbno
				+ "' and a.stb_status=0 and b.camera_status=0 and c.area_status=0 and c.area_level =1 UNION select area_id from pme_basis_area where area_id in(select c.area_pid from pme_basis_stb a,pme_basis_camera b,pme_basis_area c,pme_basis_stb_camera d where a.stb_id = d.stb_id and b.camera_id=d.camera_id and b.area_id=c.area_id and a.stb_no='"
				+ stbno
				+ "' and a.stb_status=0 and b.camera_status=0 and c.area_status=0 and c.area_level =2)) area1 where area.area_id=area1.area_id order by area.area_sort";
		List<PMEArea> areas = areadao.getBySqlL(sql, start, size);
		List<AreaVo> vos = new ArrayList<AreaVo>();
		for (PMEArea pmeArea : areas) {
			AreaVo vo = new AreaVo();
			BeanUtils.copyProperties(pmeArea, vo);
			vos.add(vo);
		}
		return vos;
	}

	/**
	 * 实现获取机顶盒拥有的镇级数量
	 */
	@Override
	public Long countTownByStbV1(String stbno) throws Exception {
		String sql = "select count(1) from pme_basis_area area ,(select c.area_id from pme_basis_stb a,pme_basis_camera b,pme_basis_area c,pme_basis_stb_camera d where a.stb_id = d.stb_id and b.camera_id=d.camera_id and b.area_id=c.area_id and a.stb_no='"
				+ stbno
				+ "' and a.stb_status=0 and b.camera_status=0 and c.area_status=0 and c.area_level =1 UNION select area_id from pme_basis_area where area_id in(select c.area_pid from pme_basis_stb a,pme_basis_camera b,pme_basis_area c,pme_basis_stb_camera d where a.stb_id = d.stb_id and b.camera_id=d.camera_id and b.area_id=c.area_id and a.stb_no='"
				+ stbno
				+ "' and a.stb_status=0 and b.camera_status=0 and c.area_status=0 and c.area_level =2)) area1 where area.area_id=area1.area_id";
		return (long) areadao.countBySql(sql).intValue();
	}

	/**
	 * 实现获取机顶盒区域
	 */
	@Override
	public AreaVo getByStbV1(String stbno) throws Exception {
		PMESTB stb = stbdao.get("from PMESTB where no = '" + stbno + "' and status = 0");
		AreaVo vo = null;
		if (stb != null) {
			PMEArea area = areadao.get("from PMEArea where status=0 and id=" + stb.getArea().getId());
			PMEAreaCms ac = acdao.get("from PMEAreaCms where areakey='zcdw' and areaId=" + area.getId());
			vo = new AreaVo();
			BeanUtils.copyProperties(area, vo);
			if (null != ac) {
				vo.setCmskey(ac.getCmsKey());
			}
		}
		return vo;
	}

	/**
	 * 实现获取镇级区域
	 */
	@Override
	@SuppressWarnings("null")
	public List<AreaVo> getChildByStbV1(String stbno, String pid, String start, String size) {
		List<PMEArea> areaArr = new ArrayList<PMEArea>();
		if (start != null || !start.equals("")) {
			areaArr = areadao.findP("from PMEArea where status=0 and pid=" + pid, Integer.parseInt(start),
					Integer.parseInt(size));
		} else {
			areaArr = areadao.find("from PMEArea where status=0 and pid=" + pid);
		}
		List<AreaVo> vos = new ArrayList<AreaVo>();
		for (PMEArea pmeArea : areaArr) {
			PMEAreaCms ac = acdao.get("from PMEAreaCms where areakey='zcdw' and areaId=" + pmeArea.getId());
			AreaVo vo = new AreaVo();
			BeanUtils.copyProperties(pmeArea, vo);
			if (null != ac) {
				vo.setCmskey(ac.getCmsKey());
			}
			vos.add(vo);
		}
		return vos;
	}

	/**
	 * 实现获取镇级区域数
	 */
	@Override
	public Long countChildByStbV1(String stbno, String countyid) {
		return areadao.count("select count(*) from PMEArea where status=0 and pid=" + countyid);
	}

}
